package com.wing.order.controller.app;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.order.model.response.InventoryInoutJobVO;
import com.wing.order.model.response.InventoryJobLabelVO;
import com.wing.order.model.response.InventoryJobProductVO;
import com.wing.order.model.response.InventoryOutPlanDetailVO;
import com.wing.order.service.*;
import com.wing.warehouse.model.response.PalletBoxListVO;
import com.wing.warehouse.service.PalletBoxListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 *
 * @author
 */
@Slf4j
@Validated
@Api(value = "App-调拨出库作业记录列表", tags = "#/order/inventoryNewOutWorkRecord")
@RestController("appInventoryOutWorkRecordController")
@RequestMapping("order/app/inventoryNewOutWorkRecord")
@RequiredArgsConstructor
public class InventoryOutWorkRecordController {

    private final InventoryOutPlanService inventoryOutPlanService;
    private final InventoryOutPlanDetailService inventoryOutPlanDetailService;
    private final PalletBoxListService palletBoxListService;
    private final InventoryInoutJobService inventoryInoutJobService;
    private final InventoryJobLabelService inventoryJobLabelService;
    private final InventoryJobProductService inventoryJobProductService;
    @ApiOperation(value = "查询所有调拨出库作业记录", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<InventoryInoutJobVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,InventoryInoutJobVO inventoryInoutJobVO
    ){

        //搜索
        IPage<InventoryInoutJobVO> list = inventoryOutPlanService.getInventoryOutWorkRecordPageList(inventoryInoutJobVO,page,limit);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }
    @ApiOperation(value = "查询出库计划商品信息", notes = "查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planId", value = "出库计划id", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping("/getInventoryOutPlanDetailVOList")
    public JsonResult<List<InventoryOutPlanDetailVO>> getInventoryOutPlanDetailVOList(@RequestParam(value = "planId") Long planId) {
        List<InventoryOutPlanDetailVO> list = inventoryOutPlanDetailService.getInventoryOutPlanDetailVOList(planId);
        return JsonResult.success(list);
    }

    @ApiOperation(value = "查询出库作业产品料箱", notes = "查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuCode", value = "产品skuCode", required = true, dataType = "String", paramType = "path")
            ,@ApiImplicitParam(name = "warehouseId", value = "仓库id", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping("/getOutWorkPalletBoxList")
    public JsonResult<List<PalletBoxListVO>> getOutWorkPalletBoxList(String skuCode, Long warehouseId) {
        List<PalletBoxListVO> list = palletBoxListService.getOutWorkPalletBoxList(skuCode,warehouseId);
        return JsonResult.success(list);
    }

    @ApiOperation(value = "添加出库作业单记录", notes = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planId", value = "计划id", required = true, dataType = "Long", paramType = "path")
            ,@ApiImplicitParam(name = "remark", value = "入库作业说明", required = false, dataType = "string", paramType = "path")
            ,@ApiImplicitParam(name = "inWarehouseId", value = "目标仓库id", required = true, dataType = "Long", paramType = "path")
            ,@ApiImplicitParam(name = "inWarehouseName", value = "目标仓名称", required = true, dataType = "string", paramType = "path")
            ,@ApiImplicitParam(name = "outWarehouseName", value = "出货仓库名称", required = true, dataType = "string", paramType = "path")
            ,@ApiImplicitParam(name = "transportType", value = "运输方式:logistics(0,物流运输);express(1,快递运输);self(2,自行运输)", required = true, dataType = "string", paramType = "path")
            ,@ApiImplicitParam(name = "companyId", value = "物流企业id", required = true, dataType = "Long", paramType = "path")
            ,@ApiImplicitParam(name = "expressCode", value = "物流单号", required = true, dataType = "string", paramType = "path")
            ,@ApiImplicitParam(name = "carNo", value = "车牌号", required = true, dataType = "string", paramType = "path")
            ,@ApiImplicitParam(name = "customerConcat", value = "联系人", required = true, dataType = "string", paramType = "path")
            ,@ApiImplicitParam(name = "customerMoble", value = "电话", required = true, dataType = "string", paramType = "path")
            ,@ApiImplicitParam(name = "jobLabelList", value = "出库库条码数据", required = true, dataType = "List", paramType = "path")
            ,@ApiImplicitParam(name = "detailList", value = "出库产品数据", required = true, dataType = "List", paramType = "path")

    })
    @PostMapping("/addOutWork")
    public JsonResult addOutWork(@RequestBody InventoryInoutJobVO inventoryInoutJobVO) {
        try {
            inventoryInoutJobService.addOutWork(inventoryInoutJobVO);
            return JsonResult.success("操作成功！");
        }catch (Exception e){
            log.error("addOutWork:", e);
            return JsonResult.fail("操作失败:" + e.getMessage());
        }
    }

    @ApiOperation(value = "查询出库产品信息详情", notes = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobId", value = "出库单id", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping("/getOutWorkProductDetail")
    public JsonResult getOutWorkProductDetail(@RequestParam(value = "jobId") Long jobId){
        List<InventoryJobProductVO> productVOList = inventoryJobProductService.getOutWorkProductDetail(jobId);
        for (InventoryJobProductVO item : productVOList){
            item.setTotalMoney(new BigDecimal(item.getQuantity()).multiply(item.getTotal()).setScale(2,BigDecimal.ROUND_HALF_UP));
        }
        return JsonResult.success(productVOList);
    }

    @ApiOperation(value = "查询出库作业记录产品明细的包装码数据", notes = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobId", value = "出库单id", required = true, dataType = "Long", paramType = "path")
            ,@ApiImplicitParam(name = "jobProductId", value = "出库单产品id", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping("/getOutWorkPackList")
    public JsonResult getOutWorkPackList(@RequestParam(value = "jobId") Long jobId,@RequestParam(value = "jobProductId") Long jobProductId){
        List<InventoryJobLabelVO> outWorkPackList = inventoryJobLabelService.getOutWorkPackList(jobId,jobProductId);
        return JsonResult.success(outWorkPackList);
    }

}
